package com.wxxymakers.cklabrecruit.service;

import com.wxxymakers.cklabrecruit.entity.RegisterInfo;
import com.wxxymakers.cklabrecruit.entity.RegisterInfoExample;
import com.wxxymakers.cklabrecruit.execption.ReApplyException;
import com.wxxymakers.cklabrecruit.mapper.RegisterInfoMapper;
import com.wxxymakers.cklabrecruit.utils.PageUtils;
import io.github.biezhi.excel.plus.ExcelPlus;
import io.github.biezhi.excel.plus.exception.ExcelException;
import io.github.biezhi.excel.plus.writer.ResponseWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import javax.servlet.http.HttpServletResponse;
import java.util.Date;
import java.util.List;
import java.util.Set;

/**
 * @AUTHOR soft
 * @DATE 2018/11/4 21:53
 * @DESCRIBE 报名信息业务层
 */
@Service
public class RegisterInfoService implements IService<RegisterInfo> {

    private final EmailReturnService emailReturnService;

    private final RegisterInfoMapper mapper;

    @Autowired
    public RegisterInfoService(RegisterInfoMapper regMapper, EmailReturnService emailReturnService) {
        this.mapper = regMapper;
        this.emailReturnService = emailReturnService;
    }

    /**
     * 判断是不是已经报过名
     * @param info 报名信息
     * @return 是否 或 抛异常
     */
    public boolean isApply(RegisterInfo info) throws ReApplyException {
        String email = info.getEmail();
        String stuId = info.getStuId();
        RegisterInfoExample stuIdex = new RegisterInfoExample();
        stuIdex.createCriteria().andStuIdEqualTo(stuId);
        List<RegisterInfo> infos = mapper.selectByExample(stuIdex);
        if (!CollectionUtils.isEmpty(infos)) {
            info.setId(infos.get(0).getId());
            throw new ReApplyException(1, "该学号已参与报名");
        }
        RegisterInfoExample emailex = new RegisterInfoExample();
        emailex.createCriteria().andEmailEqualTo(email);
        infos = mapper.selectByExample(emailex);
        if (!CollectionUtils.isEmpty(infos)) {
            info.setId(infos.get(0).getId());
            throw new ReApplyException(2, "该邮箱已被使用，换个试试");
        }
        return false;
    }

    @Transactional
    public RegisterInfo add(RegisterInfo info) {
        info.setDatetime(new Date());
        int key = mapper.insertSelective(info);
        if (key != 0) {
            info.setId(key);
            return info;
        }
        return null;
    }

    /**
     * 删除用户 同时删除和该用户相关的反馈记录
     * @param id 报名用户id
     */
    @Transactional
    public RegisterInfo delete(Object id) {
        RegisterInfo info = getOne(id);
        if (null != info) {
            if (mapper.deleteByPrimaryKey((Integer) id) > 0) {
                emailReturnService.deleteByRegId(info.getId());
                return info;
            }
        }
        return null;
    }

    @Transactional
    public boolean change(RegisterInfo info) {
        return mapper.updateByPrimaryKeySelective(info) > 0;
    }

    public RegisterInfo getOne(Object id) {
        return mapper.selectByPrimaryKey((Integer) id);
    }

    public Page<RegisterInfo> getPage(int pageNo, int size, RegisterInfo info) {
        if (pageNo <= 0) pageNo = 1;
        RegisterInfoExample example = new RegisterInfoExample();
        example.setLimit(size);
        example.setOffset((pageNo - 1) * size);
        example.setOrderByClause("id desc");
        if (info != null) {
            example.createByEquals(info);
        }

        List<RegisterInfo> infos = mapper.selectByExampleWithBLOBs(example);
        if (CollectionUtils.isEmpty(infos)) {
            return null;
        }
        long total = mapper.countByExample(example);
        return PageUtils.page(infos, pageNo, size, total);
    }

    public long count() {
        return mapper.countByExample(null);
    }

    /**
     * 修改报名信息状态
     * @param id 被修改的id
     * @param pass 是否是通过 通过状态码加一
     * @return 成功与否
     */
    public boolean changeStatus(Integer id, boolean pass) {
        if (pass) {
            return mapper.pass(id) > 0;
        } else {
            return mapper.inject(id) > 0;
        }
    }

    /**
     * 获取所有数据
     */
    public List<RegisterInfo> getAll(RegisterInfo info) {
        RegisterInfoExample example = null;
        if (info != null) {
            example = new RegisterInfoExample();
            example.setOrderByClause("id desc");
            example.createByEquals(info);
        }
        return mapper.selectByExampleWithBLOBs(example);
    }

    public Set<String> getYears() {
        return mapper.getYears();
    }

    public void exportExcel(HttpServletResponse response, RegisterInfo info) throws ExcelException {
        ExcelPlus excelPlus = new ExcelPlus();
        excelPlus.export(this.getAll(info))
                .writeAsResponse(ResponseWrapper.create(response, "用户信息表.xls"));
    }
}
